package com.coollang.smartbra.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.DisplayUtils;
import com.coollang.smartbra.utils.LogUtils;
import com.coollang.smartbra.utils.TextStyleUtils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.sweetalert.SweetAlertDialog;
import de.greenrobot.event.EventBus;

/**
 * @author: Timmy
 * @data: 2017/12/16
 * @Fuction:
 */

public class MeasureDialog extends Dialog implements View.OnClickListener {
    public static final String TAG = "MeasureDialog";
    @BindView(R.id.measuring2)
    TextView measuring2;
    @BindView(R.id.measuring4)
    TextView measuring4;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private Context mContext;
    private boolean isFirst;
    CountDownTimer mCountDownTimer;
    public SweetAlertDialog pDialog;

    public MeasureDialog(Context context) {
        super(context, R.style.dialog);
        this.mContext = context;
    }

    @Override
    public void show() {
        Window window = this.getWindow();
        // 添加动画
        window.setWindowAnimations(R.style.dialog_enter_exit);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        window.getDecorView().setPadding(0, 0, 0, 0);
        // 以下这两句是为了保证按钮可以水平满屏
        lp.width = DisplayUtils.dip2px(mContext, 280);
        lp.height = DisplayUtils.dip2px(mContext, 400);
        // 设置显示位置
        this.onWindowAttributesChanged(lp);

        // 居底
        window.setGravity(Gravity.CENTER);
        // 设置点击外围解散
        this.setCanceledOnTouchOutside(false);
        super.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_measuring);
        ButterKnife.bind(this);
        initView();
        setLinistener();
    }

    private void setLinistener() {
        //物理返回键监听
        setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    LogUtils.e(TAG, "显示");
                    showRegistDialog();
                }
                return false;
            }

        });
    }

    /**
     * 需注册弹窗
     */
    public void showRegistDialog() {
        if (pDialog != null) {

        } else {
            showDialog();
        }
    }

    private void showDialog() {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE)
                .setTitleText(UIUtils.getString(R.string.goback))
                .setConfirmText(UIUtils.getString(R.string.ok))
                .setCancelText(UIUtils.getString(R.string.cancel))
                .showCancelButton(true)
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        pDialog = null;
                    }
                })
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        disMiss();
                        pDialog = null;
                    }
                });
        pDialog.show();
    }

    private void initView() {
        isFirst = true;
        measuring2.setText(TextStyleUtils.setTextStyle("3", UIUtils.getString(R.string.measuring2), 18, UIUtils.getColor(R.color.run_setcolor)));
        mCountDownTimer = new CountDownTimer(180000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                //刷新动画  5s数字倒计时
                tvTime.setText(TimeUtils.getFormatTimemmss(millisUntilFinished / 1000) + "");
            }

            @Override
            public void onFinish() {
                EventBus.getDefault().post(new CommonEvent("", CommonEvent.REQUESTSUEECCD, CommonEvent.CEHUANJING));
                disMiss();
            }
        }.start();
        this.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                LogUtils.e(TAG, "    EnFragment.mMeasureDialog.disMiss();");
                mCountDownTimer.cancel();
            }
        });
    }

    public void disMiss() {
        //显示测量结果到UI
        mCountDownTimer.cancel();
        dismiss();
    }

    @OnClick(R.id.measuring4)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.measuring4:
                this.dismiss();
                if (isFirst) {
                    isFirst = false;
                    ToastUtils.showToast(UIUtils.getString(R.string.countagain));
                } else {
                    disMiss();
                    //发送停止测量指令
                }
                break;
            default:
                break;
        }
    }
}
